﻿using System;
using System.Collections.Generic;
using System.Linq; 
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ecopsy.Tools;
using AgencyEntityLib.BaseEntity;
using FormControllers;
using SqlLibrary;

public partial class iSales_ContractList : iSales_Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack) 
            MyDataBind();

        OnInit();
    }

    protected void OnInit()
    {
        ctlFilterSave1.OkOnClick = ctlFilterSave1_OkOnClick;
        ctlFilterSave1.FilterType = Filter.FilterType.Contractlist ;
        ctlFilterSave1.SaveOnClick = ctlFilterSave1_SaveOnClick;
        ctlFilterSave1.AddKey = "";

        ctlFilterBar1.OnFindClik = ctlFilterBar1_OnFindClik;
        ctlFilterBar1.OnFilterListClik = ctlFilterBar1_OnFilterListClik;
        ctlFilterBar1.HintServiceMethod = "GetCotractListParams";

        ctlFilterList1.OnApp_Click = ctlFilterList1_OnApp_Click;
        ctlFilterList1.FilterType = Filter.FilterType.Contractlist;
        ctlFilterList1.AddKey = "";

        ModalPopupext_FilterSave.DataBind();
        ModalPopupext_FilterList.DataBind();
    }

    private bool ctlFilterList1_OnApp_Click(ListWhere _Where)
    {
        Where = _Where;

        //ctlFilterSave1.DataBind();
        //ctlFilterBar1.DataBind();
        //lvClients.DataBind();
        MyDataBind();
        return true;
    }

    private bool ctlFilterBar1_OnFilterListClik()
    {
        ctlFilterList1.MyDataBind();
        ModalPopupext_FilterList.Show();
        return true;
    }

    private bool ctlFilterSave1_OkOnClick()
    {
        ModalPopupext_FilterSave.DataBind();
        ModalPopupext_FilterSave.Hide();
        return true;
    }

    private ListWhere ctlFilterSave1_SaveOnClick()
    {
        return (ListWhere)Where.Clone();
    }


    private bool ctlFilterBar1_OnFindClik(string sFindStr)
    {
        Where.ListLoadStatment.Lexeme = ctlFilterBar1.Lexeme;
        MyDataBind ();
        return true;
    }


    #region SavePanel

    protected string GetTargetControlID_FilterSave()
    {
        return ctlFilterBar1.BtnAddFilter_ClientID;
    }

    public string GetOkControlID_FilterSave()
    {

        return ctlFilterSave1.btnSave_ClientID;
    }

    public string GetCancelControlID_FilterSave()
    {

        return ctlFilterSave1.BtnCancel_ClientID;
    }

    #endregion

    #region ListPanel

    protected string GetTargetControlID_FilterList()
    {
        return ctlFilterBar1.BtnFilterList_ClientID;
    }

    public string GetOkControlID_FilterList()
    {

        return ctlFilterList1.btnSave_ClientID;
    }

    public string GetCancelControlID_FilterList()
    {

        return ctlFilterList1.BtnCancel_ClientID;
    }

    #endregion






    private void MyDataBind()
    {
        ObjectDS_PF ODS = new ObjectDS_PF(GetList);
        lvContracts.DataSource = ODS.ImAsObjectDataSource;
        DataBind();

    }

    public grdContract[] GetList(int startRowIndex, int maximumRows, out int RowCount)
    {

        wsContract ws = (wsContract)GlobalConstants.CurrentAgency.WS_FactoryList[WS_FactoryList.wsKey_Contract]; 
        ListWhere w = (ListWhere)Where;
        w.ListLoadStatment.StartRowIndex = startRowIndex;
        w.ListLoadStatment.EndRowIndex = startRowIndex + maximumRows;
        ContractList  l = ws.GetContractList((ContractList.sqlLoadStatment)w.ListLoadStatment);
        RowCount = l.OutputRwCnt;

        List <grdContract> grdl= new List<grdContract> ();

        foreach (Contract item in l) grdl.Add(new grdContract(item));

        //заряжаем comparer функцией сортировки 
        Where.SortCondition.fnOrderBy = SortFunctions.fOrderBy;
        // Сортируем
        grdContract[] r = grdl.ToArray<grdContract>();
        Array.Sort(r, Where.SortCondition);
        return r;
       
    }

    protected override string Key
    {
        get
        {
            return "Contractlist";
        }        
    }
    
    protected override ListWhere Where
    {
        get
        {
            ContractList.sqlLoadStatment st = new ContractList.sqlLoadStatment();
            return f_Where(st, "Number");
        }

        set
        {
            f_SetWhere(value);
        }
    }

    protected void lvSort(object sender, EventArgs e)
    {
        f_lvSort(sender, lvContracts, MyDataBind);
        Panel pnl = (Panel)lvContracts.FindControl("pnlHeadOfList");
        pnl.DataBind();
        
    }
       
    protected void lvContracts_PagePropertiesChanged(object sender, EventArgs e)
    {
        MyDataBind();
    }

    protected override string GetHeadCss(string HearTd)
    {
       return base.GetHeadCss(HearTd);
       
        
    }

    protected string GetEditRecordCommand(string ID)
    {

        bool isEnabled = true;
        string sUrlKey = GlobalConstants.SiteMapItem_iSales_Contract;
        iPageController.SetPageUrl(ref sUrlKey, ref isEnabled);
        string s = sUrlKey + ".aspx?" + iSales_Page.qContractID + "=" + ID+"";
        return "document.location='" + s + "'";
        
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {

        wsContract ws = (wsContract)GlobalConstants.CurrentAgency.WS_FactoryList[WS_FactoryList.wsKey_Contract]; 
        Contract c= ws.GetNewContract ();
        c.ContractStatus = new ContractStatus_Advice();   
        string sUrlKey = GlobalConstants.SiteMapItem_iSales_Contract;
        string s = "PageProcessor.aspx?Page=" + sUrlKey + ".aspx?" + iSales_Page.qContractID + "=" + c.ID;
        Response.Redirect(s);

    }
}
